/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.admin;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;
import io.jboot.utils.ModelUtil;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.commons.layer.SortKit;
import io.jpress.model.*;
import io.jpress.permission.annotation.PermissionDef;
import io.jpress.service.*;
import io.jpress.web.AdminPermissionGroups;
import io.jpress.web.base.AdminControllerBase;

import java.util.List;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Title: 用户信息，用户详情 Controller
 */
@RequestMapping(value = "/admin/user/detail", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _UserInfoController extends AdminControllerBase {


    @Inject
    private RoleService roleService;

    @Inject
    private UserService userService;

    @Inject
    private PermissionService permissionService;

    @Inject
    private UtmService utmService;

    @Inject
    private UserTagService userTagService;

    @Inject
    private DepartmentService departmentService;


    /**
     * 查看用户详情
     */
    @PermissionDef(title = "查看某用户详情", groupId = AdminPermissionGroups.USER)
    public void index() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        List<Department> departments = ModelUtil.copy(departmentService.findAll());
        SortKit.toLayer(departments);
        setAttr("departments", departments);

        Long departmentId = userService.findDepartmentId(user.getId());
        setAttr("departmentId", departmentId);

        render("user/detail.html");
    }


    /**
     * 查看用户角色
     */
    @PermissionDef(title = "查看某用户角色", groupId = AdminPermissionGroups.USER)
    public void role() {

        User user = userService.findById(getParaToLong());
        setAttr("user", user);

        List<Role> roles = roleService.findAll();
        setAttr("roles", roles);

        render("user/detail_role.html");
    }

    /**
     * 查看用户标签
     */
    @PermissionDef(title = "查看某用户标签", groupId = AdminPermissionGroups.USER)
    public void tag() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        List<UserTag> userTags = userTagService.findListByUserId(uid);
        setAttr("userTags", userTags);

        List<UserTag> hotTags = userTagService.findHotList(50);
        setAttr("hotTags", hotTags);

        render("user/detail_tag.html");
    }




    /**
     * 查看用户联系方式
     */
    @PermissionDef(title = "查看某用户联系方式", groupId = AdminPermissionGroups.USER)
    public void communication() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        render("user/detail_communication.html");
    }

    /**
     * 修改用户密码
     */
    @PermissionDef(title = "修改某用户密码", groupId = AdminPermissionGroups.USER)
    public void pwd() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        render("user/detail_pwd.html");
    }


    /**
     * 查看用户签名
     */
    @PermissionDef(title = "查看某用户签名", groupId = AdminPermissionGroups.USER)
    public void signature() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        render("user/detail_signature.html");
    }


    /**
     * 查看用户其他信息
     */
    public void other() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        render("user/detail_other.html");
    }


    /**
     * 查看用户头像，编辑用户头像
     */
    @PermissionDef(title = "查看某用户头像", groupId = AdminPermissionGroups.USER)
    public void avatar() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        render("user/detail_avatar.html");
    }


    /**
     * 查看用户操作记录
     */
    @PermissionDef(title = "查看某用户操作记录", groupId = AdminPermissionGroups.USER)
    public void utm() {
        Long uid = getParaToLong();
        User user = userService.findById(uid);
        setAttr("user", user);

        Page<Utm> page = utmService._paginateByUserId(getPagePara(), 20, user.getId());
        setAttr("page", page);

        render("user/detail_utm.html");
    }


}
